package com.ourteam.product.model.api;

import org.apache.commons.lang3.StringUtils;

import com.ourteam.frame.web.AbstractSearchModel;
import com.ourteam.product.model.dao.ProductModelObjectDAOQueryBean;
import com.ourteam.product.model.domain.ProductModelObjectBean;

public class ProductModelObjectSearchModel extends AbstractSearchModel<ProductModelObjectDAOQueryBean> {

	private String objectName;

	private String objectType;

	private String excludeObjectType;

	private long domainId;

	private Long packageId;

	public Long getPackageId() {
		return packageId;
	}

	public void setPackageId(Long packageId) {
		this.packageId = packageId;
	}

	public String getObjectName() {
		return objectName;
	}

	public void setObjectName(String objectName) {
		this.objectName = objectName;
	}

	public String getObjectType() {
		return objectType;
	}

	public void setObjectType(String objectType) {
		this.objectType = objectType;
	}

	public long getDomainId() {
		return domainId;
	}

	public void setDomainId(long domainId) {
		this.domainId = domainId;
	}

	public String getExcludeObjectType() {
		return excludeObjectType;
	}

	public void setExcludeObjectType(String excludeObjectType) {
		this.excludeObjectType = excludeObjectType;
	}

	@Override
	public String getMappedDAOField(String aProperty) {
		return ProductModelObjectBean.getMappedDAOField(aProperty);
	}

	@Override
	protected void doBuildQueryBean(ProductModelObjectDAOQueryBean queryBean) throws Exception {

		ProductModelObjectDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		if (StringUtils.isNotBlank(objectName)) {
			criteria.andObjectNameLike(objectName);
		}

		if (StringUtils.isNotBlank(objectType)) {
			if (StringUtils.contains(objectType, ",")) {
				criteria.andObjectTypeIn(StringUtils.split(objectType, ","));
			} else {
				criteria.andObjectTypeEqualTo(objectType);
			}
		}

		if (StringUtils.isNotBlank(excludeObjectType)) {
			if (StringUtils.contains(excludeObjectType, ",")) {
				criteria.andObjectTypeNotIn(StringUtils.split(excludeObjectType, ","));
			} else {
				criteria.andObjectTypeNotEqualTo(excludeObjectType);
			}
		}

		if (domainId != 0) {
			criteria.andDomainIdEqualTo(domainId);
		}

		if (packageId != null && packageId != 0) {
			criteria.andPackageIdEqualTo(packageId);
		}

	}

}
